﻿package sparrowGui.item
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import sparrowGui.Component;
	import sparrowGui.skin.TagsBtnSkin;
	import sparrowGui.utils.PubFun;

	/**
	 * 列表项
	 * 有鼠标经过,选中,移开三状态颜色
	 * @author Pelephone
	 */	
	public class Item extends Component
	{
//		数据解析方式 0,自动跟椐信息解析；1.将数据当做字符解析; 2.将数据当做数组列解析；3.将数据当做对象列解析
//		public static const AUTO_PARSE:int		= 0;
//		public static const PARSE_AS_STR:int	= 1;
//		public static const PARSE_AS_ARR:int	= 2;
//		public static const PARSE_AS_OBJ:int	= 3;
		
//		protected var parseFunc:Function = null;	//项解析数据到皮肤的函数,此函数带两参数.1,数据vo;2,皮肤参数
		private var _data:Object;
		
		protected var _selected:Boolean;
		private var _index:int;
		
		
		public function Item(data:Object=null,argSkin:Sprite=null,vars:Object=null)
		{
			_skin = argSkin || new TagsBtnSkin();
			addChild(skin);
//			skin.x = skin.y = 0;
			if(skin.hasOwnProperty('hitTestState')) hitArea = hitTestState;
			super(vars);
//			parseFunc = parseFun;
			update(data);
			
			addEvt();
//			addEventListener(Event.ADDED_TO_STAGE,onStageEvt);
//			addEventListener(Event.REMOVED_FROM_STAGE,onStageEvt);
		}
		
		protected function addEvt():void
		{
			addEventListener(MouseEvent.MOUSE_OUT, onMouse);
			addEventListener(MouseEvent.ROLL_OVER, onMouse);
			addEventListener(MouseEvent.MOUSE_DOWN, onMouse);
		}
		
		protected function removeEvt():void
		{
			removeEventListener(MouseEvent.MOUSE_OUT, onMouse);
			removeEventListener(MouseEvent.ROLL_OVER, onMouse);
			removeEventListener(MouseEvent.MOUSE_DOWN, onMouse);
		}
		
/*		private function onStageEvt(e:Event):void
		{
			switch(e.type){
				case Event.ADDED_TO_STAGE:
					// 如果overVar没设置，为空的话则不添加帧听鼠标经过变色事件
					if(!skin || !skin.hasOwnProperty("overState")) return;
					addEventListener(MouseEvent.MOUSE_OUT, onMouse);
					addEventListener(MouseEvent.ROLL_OVER, onMouse);
					addEventListener(MouseEvent.MOUSE_DOWN, onMouse);
					break;
				case Event.REMOVED_FROM_STAGE:
					
					destroy();
					break;
			}
		}*/
		
		override protected function init():void
		{
			super.init();
			buttonMode = true;
			useHandCursor = true;
			mouseEnabled  = true;
			mouseChildren = false;
		}
		
		override protected function draw():void
		{
			if(style.isBgFill && skin){
				skin.width = style.width;
				skin.height = style.height;
			}
//			if(!_selected) showClip();
//			else showClip(1);
			showOutState();
			
			super.draw();
		}
		
		/**
		 * 更新数据
		 */
		public function update(o:Object):void
		{
			_data = o;
			
//			if(parseFunc!=null)
//				parseFunc(_data,skin);
		}
		
		public function onMouse(e:Event):void
		{
			switch(e.type){
				case MouseEvent.ROLL_OVER:
					if(selected) return;
					if(!downState || !downState.visible)
						showClip(2);
					break;
				case MouseEvent.MOUSE_DOWN:
					if(selected) return;
					showClip(3);
					if(!stage) return;
					stage.addEventListener(MouseEvent.MOUSE_UP, onMouse);
					stage.addEventListener(Event.MOUSE_LEAVE,onMouse);
					break;
				case MouseEvent.MOUSE_UP:
				case Event.MOUSE_LEAVE:
					if(selected) return;
					showOutState();
					if(!stage) return;
					stage.removeEventListener(MouseEvent.MOUSE_UP, onMouse);
					stage.removeEventListener(Event.MOUSE_LEAVE, onMouse);
					break;
				case MouseEvent.MOUSE_OUT:
					if(!downState || !downState.visible)
						showOutState();
					break;
			}
		}
		
		// 一扩展接口,自动按List的指定的参数填满
		public function autoFill(arg:Object=null):void
		{
		}
		
		private function showOutState():void
		{
			if(_selected && selectState){
				showClip(1);
			}else{
//				removeEvt();
//				addEvt();
				showClip(0);
			}
		}
		
		///////////////////////////////////
		// getter/setters
		///////////////////////////////////
		
		public function set selected(value:Boolean):void
		{
			_selected = value;
			showOutState();
		}
		
		public function get selected():Boolean
		{
			if(!selectState) return false;
			return _selected;
		}
		
		public function get index():int
		{
			return _index;
		}
		
		public function get itemSkin():Sprite
		{
			return skin;
		}

		public function set index(value:int):void
		{
			_index = value;
		}

		public function get itemData():Object
		{
			return _data;
		}

		public function set itemData(value:Object):void
		{
			_data = value;
		}

/*		public function get itemTag():String
		{
			return style.itemTag;
		}*/
		
		override public function set width(w:Number):void
		{
			super.width = w;
			skin.width = w;
		}
		
		override public function set height(h:Number):void
		{
			super.height = h;
			skin.height = h;
		}
		
		private function showClip(id:int=0):void
		{
			var arr:Array = [upState,selectState,overState,downState,hitTestState];
			hiddenClip();
			
			if(arr[id]) arr[id].visible = true;
		}
		
		private function hiddenClip():void
		{
			var arr:Array = [upState,selectState,overState,downState,hitTestState];
			for each(var dp:DisplayObject in arr)
				if(dp) dp.visible = false;
		}
		
		override public function dispose():void
		{
			removeEventListener(MouseEvent.MOUSE_OUT, onMouse);
			removeEventListener(MouseEvent.ROLL_OVER, onMouse);
			removeEventListener(MouseEvent.MOUSE_DOWN, onMouse);
			if(stage){
				stage.removeEventListener(MouseEvent.MOUSE_UP, onMouse);
				stage.removeEventListener(Event.MOUSE_LEAVE, onMouse);
			}
			
//			parseFunc = null;
			_data = null;
			super.dispose();
		}
		
		public function get upState():Sprite{ return skin["upState"] as Sprite };
		public function get selectState():Sprite{ return skin["selectState"] as Sprite };
		public function get overState():Sprite{ return skin["overState"] as Sprite };
		public function get downState():Sprite{ return skin["downState"] as Sprite };
		public function get hitTestState():Sprite{ return skin["hitTestState"] as Sprite };
	}
}